<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="订单编号" prop="orderNumber">
        <el-input
          v-model="queryParams.orderNumber"
          placeholder="请输入订单编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="样品名称" prop="simpleTypeName">
        <el-input
          v-model="queryParams.simpleTypeName"
          placeholder="请输入样品名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报告状态" prop="state">
        <el-select
          v-model="queryParams.state"
          placeholder="报告状态"
          clearable
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="dict in stateOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--<el-col :span="1.5">-->
        <!--<el-button-->
          <!--type="primary"-->
          <!--icon="el-icon-plus"-->
          <!--size="mini"-->
          <!--@click="handleAdd"-->
          <!--v-hasPermi="['simple:orderDetail:add']"-->
        <!--&gt;新增-->
        <!--</el-button>-->
      <!--</el-col>-->
      <!--<el-col :span="1.5">-->
        <!--<el-button-->
          <!--type="success"-->
          <!--icon="el-icon-edit"-->
          <!--size="mini"-->
          <!--:disabled="single"-->
          <!--@click="handleUpdate"-->
          <!--v-hasPermi="['simple:orderDetail:edit']"-->
        <!--&gt;修改-->
        <!--</el-button>-->
      <!--</el-col>-->
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['simple:orderDetail:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['simple:orderDetail:export']"
        >导出
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="orderDetailList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="订单编号" align="center" prop="order.orderNumber"/>
      <el-table-column label="样品编号" align="center" prop="orderNumber"/>
      <el-table-column label="样品名称" align="center" prop="simpleTypeName"/>
      <el-table-column label="客户姓名" align="center" prop="order.submitName"/>
      <el-table-column label="联系方式" align="center" prop="order.submitPhone"/>
      <el-table-column label="外包装是否完好" align="center" prop="packingIntact" :formatter="packingIntactFormat"/>
      <el-table-column label="急性肝胰腺坏死病" align="center" prop="ahpnsEms" :formatter="ahpnsEmsFormat"/>
      <el-table-column label="对虾白班浊病" align="center" prop="wsd" :formatter="wsdFormat"/>
      <el-table-column label="皮下及造血组织坏死病毒" align="center" prop="ihhnv" :formatter="ihhnvFormat"/>
      <el-table-column label="肛肠鲍冲" align="center" prop="ehp" :formatter="ehpFormat"/>
      <el-table-column label="虾血虹彩病毒(SHIV)" align="center" prop="shiv" :formatter="shivFormat"/>

      <el-table-column label="状态" align="center" prop="state">
        <template slot-scope="scope">
          <span v-if="scope.row.state==10" class="">待受理</span>
          <span v-if="scope.row.state==20">待生成</span>
          <span v-if="scope.row.state==30">待复核</span>
          <span v-if="scope.row.state==40">待付款</span>
          <span v-if="scope.row.state==50">已付款</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!--<el-button-->
            <!--size="mini"-->
            <!--type="text"-->
            <!--icon="el-icon-edit"-->
            <!--@click="handleUpdate(scope.row)"-->
            <!--v-hasPermi="['simple:orderDetail:edit']"-->
          <!--&gt;修改-->
          <!--</el-button>-->
          <el-button
            v-if="scope.row.state==30"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleReviewUpdate(scope.row)"
            v-hasPermi="['simple:orderDetail:review']"
          >复核
          </el-button>
          <el-button
            size="mini"
            type="text"
            v-if="scope.row.state==40"
            icon="el-icon-edit"
            @click="handlePayedUpdate(scope.row)"
            v-hasPermi="['simple:orderDetail:payed']"
          >确认付款
          </el-button>
          <el-button
            size="mini"
            type="text"
            v-if="scope.row.state<50"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['simple:orderDetail:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改样品订单明细对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单明细编号" prop="orderNumber">
          <el-input v-model="form.orderNumber" placeholder="请输入订单明细编号"/>
        </el-form-item>
        <el-form-item label="样品订单ID" prop="orderId">
          <el-input v-model="form.orderId" placeholder="请输入样品订单ID"/>
        </el-form-item>
        <el-form-item label="样品订单ID" prop="simpleTypeId">
          <el-input v-model="form.simpleTypeId" placeholder="请输入样品订单ID"/>
        </el-form-item>
        <el-form-item label="样品名称" prop="simpleTypeName">
          <el-input v-model="form.simpleTypeName" placeholder="请输入样品名称"/>
        </el-form-item>
        <el-form-item label="样品图片" prop="simpleTypeCover">
          <el-input v-model="form.simpleTypeCover" placeholder="请输入样品图片"/>
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-input v-model="form.state" placeholder="请输入状态"/>
        </el-form-item>
        <el-form-item label="删除标志" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标志"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listOrderDetail,
    getOrderDetail,
    delOrderDetail,
    addOrderDetail,
    updateOrderDetail,
    exportOrderDetail
  } from "@/api/simple/orderDetail";

  export default {
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        stateOptions: [{dictLabel: "待受理", dictValue: "10"}, {dictLabel: "待生成", dictValue: "20"}, {
          dictLabel: "待复核",
          dictValue: "30"
        }, {dictLabel: "待付款", dictValue: "40"}, {dictLabel: "已付款", dictValue: "50"}],
        // 样品订单明细表格数据
        orderDetailList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 对虾包装是否完好字典
        packingIntactOptions: [],
        // 急性肝胰腺坏死病字典
        ahpnsEmsOptions: [],
        // 对虾白班浊病字典
        wsdOptions: [],
        // 皮下及造血组织坏死病毒字典
        ihhnvOptions: [],
        // 肛肠鲍冲字典
        ehpOptions: [],
        // 虾血虹彩病毒(SHIV)字典
        shivOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          orderNumber: undefined,
          orderId: undefined,
          simpleTypeId: undefined,
          simpleTypeName: undefined,
          simpleTypeCover: undefined,
          state: undefined,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {}
      };
    },
    created() {
      this.getList();

      this.getDicts("sys_yes_no").then(response => {
        this.packingIntactOptions = response.data;
      });
      this.getDicts("spl_positive_negative").then(response => {
        this.ahpnsEmsOptions = response.data;
      });
      this.getDicts("spl_positive_negative").then(response => {
        this.wsdOptions = response.data;
      });
      this.getDicts("spl_positive_negative").then(response => {
        this.ihhnvOptions = response.data;
      });
      this.getDicts("spl_positive_negative").then(response => {
        this.ehpOptions = response.data;
      });
      this.getDicts("spl_positive_negative").then(response => {
        this.shivOptions = response.data;
      });
    },
    methods: {
      /** 查询样品订单明细列表 */
      getList() {
        this.loading = true;
        listOrderDetail(this.queryParams).then(response => {
          this.orderDetailList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 对虾包装是否完好字典翻译
      packingIntactFormat(row, column) {
        return this.selectDictLabel(this.packingIntactOptions, row.report.packingIntact);
      },
      // 急性肝胰腺坏死病字典翻译
      ahpnsEmsFormat(row, column) {
        return this.selectDictLabel(this.ahpnsEmsOptions, row.report.ahpnsEms);
      },
      // 对虾白班浊病字典翻译
      wsdFormat(row, column) {
        return this.selectDictLabel(this.wsdOptions, row.report.wsd);
      },
      // 皮下及造血组织坏死病毒字典翻译
      ihhnvFormat(row, column) {
        return this.selectDictLabel(this.ihhnvOptions, row.report.ihhnv);
      },
      // 肛肠鲍冲字典翻译
      ehpFormat(row, column) {
        return this.selectDictLabel(this.ehpOptions, row.report.ehp);
      },
      // 虾血虹彩病毒(SHIV)字典翻译
      shivFormat(row, column) {
        return this.selectDictLabel(this.shivOptions, row.report.shiv);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          orderDetailId: undefined,
          orderNumber: undefined,
          orderId: undefined,
          simpleTypeId: undefined,
          simpleTypeName: undefined,
          simpleTypeCover: undefined,
          state: undefined,
          delFlag: undefined,
          createBy: undefined,
          createTime: undefined,
          updateBy: undefined,
          updateTime: undefined,
          remark: undefined
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.orderDetailId)
        this.single = selection.length != 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加样品订单明细";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const orderDetailId = row.orderDetailId || this.ids
        getOrderDetail(orderDetailId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改样品订单明细";
        });
      },
      //报告复核
      handleReviewUpdate(row) {
        this.$confirm('是否确认样品单号为"' + row.orderNumber + '"的报告无误?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          let orderDetail = {
            orderDetailId: row.orderDetailId,
            state:40
          }
          return updateOrderDetail(orderDetail);
        }).then(() => {
          this.getList();
          this.msgSuccess("报告确认无误");
        }).catch(function () {
        });
      },
      //报告确认付款
      handlePayedUpdate(row) {
        this.$confirm('是否确认样品单号为"' + row.orderNumber + '"的报告已付款?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          let orderDetail = {
            orderDetailId: row.orderDetailId,
            state:50
          }
          return updateOrderDetail(orderDetail);
        }).then(() => {
          this.getList();
          this.msgSuccess("报告确认付款成功");
        }).catch(function () {
        });
      },
      /** 提交按钮 */
      submitForm: function () {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.orderDetailId != undefined) {
              updateOrderDetail(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                } else {
                  this.msgError(response.msg);
                }
              });
            } else {
              addOrderDetail(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                } else {
                  this.msgError(response.msg);
                }
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const orderDetailIds = row.orderDetailId || this.ids;
        this.$confirm('是否确认删除样品订单明细编号为"' + orderDetailIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return delOrderDetail(orderDetailIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function () {
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有样品订单明细数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return exportOrderDetail(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function () {
        });
      }
    }
  };
</script>

